#!/bin/bash

#采集cpu的信息
host_name=$(hostname)
cpu_idle_percent=$(top -bn 1|grep "^%Cpu" |awk -F'[ ,]+'  '{print $8}')
cpu_used_percent=$(echo "100 - $cpu_idle_percent"|bc)
echo "$cpu_idle_percent  $cpu_used_percent"

#cpu信息入库
# sql
insert_cpu_data="insert into cpuinfo(data_time,machine_name,used_percent,idle_percent) values(now(),'$host_name','$cpu_used_percent','$cpu_idle_percent')"

mysql -h 192.168.100.152 -usc -p'sc123456' monitor -e "$insert_cpu_data"

#mem info
free_num=$(free -m|grep "^Mem"|awk '{print $4}')
total_num=$( free -m|grep "^Mem"|awk '{print $2}')
mem_used_percent=$(echo "scale=2;100 - $free_num *100 / $total_num"|bc)
insert_mem_data="insert into meminfo(data_time,machine_name,used_mem_percent,free_mem,total_mem) values(now(),'$host_name','$mem_used_percent','$free_num','$total_num')"

mysql -h 192.168.100.152 -usc -p'sc123456' monitor -e "$insert_mem_data"




